﻿using System;
using System.Collections.Generic;
using System.Data.Objects;
using System.Data.EntityClient;
using System.Data;
using System.Linq;

namespace PotatoSoft
{
    namespace VOCTS
    {
        namespace Workbench
        {
            public class Workbench
            {
                #region Properties

                private WorkbenchContext _context;
                public bool PendingChanges { get; set; }

                #endregion  

                #region Constructor

                public Workbench()
                {
                    _context = new WorkbenchContext();
                    
                    PendingChanges = false;
                }

                #endregion

                #region Methods
                public void Dispose()
                {
                    _context.Dispose();
                }
                
                public void NewProject(Project pr)
                {
                    _context.AddObject("Projects", pr);
                    PendingChanges = true;
                }

                // w przyszlosci
                //public void OpenProject(Project pr)
                //{                    
                //}

                public void DeleteProject(Project pr)
                {
                    _context.DeleteObject(pr);
                    PendingChanges = true;
                }

                public void AddExaminationToProject(Examination ex)
                {
                    _context.AddObject("Examinations", ex);
                    PendingChanges = true;
                }

                public void DeleteImageFromExamination(Examination ex)
                {
                    _context.DeleteObject(ex);
                    PendingChanges = true;
                }

                public void AddImageToExamination(WorkbenchImage im)
                {
                    _context.AddObject("Images", im);
                    PendingChanges = true;
                }

                public void DeleteImageFromExamination(WorkbenchImage im)
                {
                    _context.DeleteObject(im);
                    PendingChanges = true;
                }

                public void SaveToDatabase()
                {
                    if (0 < _context.SaveChanges())
                    {
                        PendingChanges = false;
                    }
                }

                #endregion

                #region Queries

                public WorkspaceMetadata GetMetadata(int wid = 0)
                {
                    WorkspaceMetadata wm = new WorkspaceMetadata(
                        wid, 
                        _context.Workspaces.Single(ws => ws.WorkspaceID == wid).WorkspaceName);
                    
                    var pquery = _context.Projects.Where(p => p.WorkspaceID == wid).OrderBy(pr => pr.ProjectID);
                    foreach (Project pr in pquery)
                    {
                        wm.WorkspaceProjects.Add(
                            new ProjectMetadata(pr.ProjectID, pr.ProjectName));

                        var equery = _context.Examinations.Where(ex => ex.ProjectID == pr.ProjectID);
                        foreach (Examination ex in equery)
                        {
                            int pIndex = wm.WorkspaceProjects.Count - 1;
                            wm.WorkspaceProjects[pIndex].ProjectExaminations.Add(
                                new ExaminationMetadata(ex.ExaminationID, ex.ExaminationName));

                            var iquery = _context.Images.Where(im => im.ExaminationID == ex.ExaminationID);
                            foreach (WorkbenchImage im in iquery)
                            {
                                int eIndex = wm.WorkspaceProjects[pIndex].ProjectExaminations.Count - 1;
                                wm.WorkspaceProjects[pIndex].ProjectExaminations[eIndex].ExaminationImages.Add(
                                    new ImageMetadata(im.ImageID, im.ImageName));
                            }
                        }
                    }

                    return wm;
                }

                public WorkbenchImage GetImageByMetadata(int iid)
                {
                    return _context.Images.Single(im => im.ImageID == iid);
                }

                #endregion

            }
        }
    }
}
